package com.xu.wemall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xu.wemall.entry.Goods;
import com.xu.wemall.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p >
 *
 * @author astupidcoder
 * @since 2020-05-13
 */

@Slf4j
@Api(tags = "商品操作接口")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService iGoodsService;

    @ApiOperation("分页查询商品")
    @GetMapping("/pageList")
    public IPage<Goods> pageList(Integer pageSize, Integer pageNo) {
        IPage<Goods> iPage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
//        if(goods != null) {
//            wrapper.like(StringUtils.isNotBlank(goods.getTitle()),
//                    Goods::getTitle, goods.getTitle());
//        }
        return iGoodsService.page(iPage, wrapper);
    }

    @ApiOperation("添加")
    @PostMapping("/insert")
    public Boolean inserGoods(@RequestBody Goods goods) {
        // count是新增的条数
        Boolean aBoolean = iGoodsService.save(goods);
        return aBoolean;
    }

    @ApiOperation("商品详情")
    @GetMapping("/getById")
    public Goods getById(Long id) {
        // count是新增的条数
        return iGoodsService.getById(id);
    }

    @ApiOperation("分页查询商品")
    @PostMapping("/pageList")
    public IPage<Goods> pageList(@RequestParam Integer pageSize, @RequestParam Integer pageNo,
                                 @RequestBody Goods goods) {
        IPage<Goods> iPage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        if (goods != null) {
            wrapper.like(StringUtils.isNotBlank(goods.getTitle()),
                    Goods::getTitle, goods.getTitle());
        }
        return iGoodsService.page(iPage, wrapper);
    }
}
